/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vetxsoft.bean;

import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import vetxsoft.entity.Cliente;
import vetxsoft.entity.Consulta;
import vetxsoft.entity.Control;
import vetxsoft.entity.HistoriaClinica;
import vetxsoft.entity.Mascota;
import vetxsoft.system.IVetexsoftSystem;

/**
 *
 * @author Nother
 */
@ManagedBean
@SessionScoped
public class ControlBean {
    @EJB
    private IVetexsoftSystem vetexsoftSystem;
    private Control control = new Control();
    private Cliente cliente;
    private Mascota mascota = new Mascota();
    private HistoriaClinica historiaClinica = new HistoriaClinica();
    private Consulta consulta = new Consulta();
    private Date currentDate = new Date();
    private boolean stored = false;

    public Control getControl() {
        return control;
    }



    public Cliente getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    public Mascota getMascota() {
        return mascota;
    }

    public void setMascota(Mascota mascota) {
        this.mascota = mascota;
    }

    public IVetexsoftSystem getVetexsoftSystem() {
        return vetexsoftSystem;
    }

    public Date getCurrentDate() {
        return currentDate;
    }

    public boolean isStored() {
        return stored;
    }

    public HistoriaClinica getHistoriaClinica() {
        return historiaClinica;
    }

    public void setHistoriaClinica(HistoriaClinica historiaClinica) {
        this.historiaClinica = historiaClinica;
    }

    public Consulta getConsulta() {
        return consulta;
    }

    public void setConsulta(Consulta consulta) {
        this.consulta = consulta;
    }
    
    

    
    public void cancelSelectionCliente() {
        this.cliente = (null);
        this.consulta = (null);
        this.historiaClinica = (null);
    }
    
    public void saveAction(){
        try {
            //Guardamos el control
            this.control.setStored(this.stored);
            this.vetexsoftSystem.saveControl(this.control);
        // obtenemos la ultimo control guardada, la cual nos proporcionará el codigo generado automaticamente
            List<Control> facturas = this.vetexsoftSystem.obtenerControl();
            this.control = facturas.get(facturas.size() - 1);
            
             //guardamos la historia clinica
            this.vetexsoftSystem.saveHistoriaClinica(this.historiaClinica);
            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "El control ha sido registrado satisfactoriamente.", ""));
            this.consulta = new Consulta();
            this.control = new Control();
            this.cliente = null;
            this.historiaClinica = new HistoriaClinica();
            this.stored = false;
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "No se ha podido registrar el control. Por favor, vuelva a intentarlo.", ""));
        }
    }
    
    public void cancelAction(){
        this.control = new Control();
        this.cliente = null;
        this.historiaClinica = new HistoriaClinica();
        this.consulta = new Consulta();
        this.stored = false;
    }
    
    
}
